<?php
namespace Models\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;

class ComplaintsTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function postComplaint($complaint)
    {
		$data = array(
			'complaint_from_id' 	=> $complaint['user_id'], 			
			'complaint'  	 		=> $complaint['complaint'],  	
			'complaint_on_id' 	 	=> $complaint['tracking_id'],  
			'status'	 			=> 0, 				
			'date_added'	 		=> date('Y-m-d H:i:s'), 				
			'updated_date'	 		=> date('Y-m-d H:i:s'), 				
		);	
		$insertresult=$this->tableGateway->insert($data);	
		return $this->tableGateway->lastInsertValue;
    }	
	public function getComplaints($user_id){
		$select = $this->tableGateway->getSql()->select();
		$select->join('complaint_relpies', new Expression('complaint_relpies.complaint_id=complaints.complaint_id'),array('reply_text','complaint_relpier_id','complaint_relpies_id'),'left');
		$select->join('user_medicine_order', new Expression('user_medicine_order.user_medicine_order_id=complaints.complaint_on_id'),array('*'),'left');
		$select->join('user', new Expression('user.user_id=complaint_relpies.complaint_relpier_id'),array('shop_id'=>'user_id','shop_name'=>'user_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('city','state','country','address'),'left');	
		$select->where('user_medicine_order.user_id="'.$user_id.'"');
		$alllist = $this->tableGateway->selectWith($select);	
		return $alllist;
	}
	public function getShopComplaints($shop_id){
		$select = $this->tableGateway->getSql()->select();
		$select->join('complaint_relpies', new Expression('complaint_relpies.complaint_id=complaints.complaint_id'),array('reply_text','complaint_relpier_id','complaint_relpies_id'),'left');
		$select->join('user_medicine_order', new Expression('user_medicine_order.user_medicine_order_id=complaints.complaint_on_id'),array('*'),'left');
		$select->join('order_delivery_details', new Expression('user_medicine_order.user_medicine_order_id=order_delivery_details.user_medicine_order_id'),array('*'),'left');
		$select->join('user', new Expression('user.user_id=user_medicine_order.user_id'),array('user_id','user_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('city','state','country','address'),'left');	
		$select->where('order_delivery_details.shop_id="'.$shop_id.'"');
		$alllist = $this->tableGateway->selectWith($select);	
		return $alllist;
	}
	public function getAllComplaints()
	{
		$select = $this->tableGateway->getSql()->select();
		$select->join('complaint_relpies', new Expression('complaint_relpies.complaint_id=complaints.complaint_id'),array('reply_text','complaint_relpier_id','complaint_relpies_id'),'left');
		$select->join('user_medicine_order', new Expression('user_medicine_order.user_medicine_order_id=complaints.complaint_on_id'),array('*'),'left');
		$select->join('order_delivery_details', new Expression('user_medicine_order.user_medicine_order_id=order_delivery_details.user_medicine_order_id'),array('*'),'left');
		$select->join('user', new Expression('user.user_id=user_medicine_order.user_id'),array('user_id','user_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('city','state','country','address'),'left');	
		$select->join(array('shop'=>'user'), new Expression('shop.user_id=order_delivery_details.shop_id'),array('shop_id'=>'user_id','shop_name'=>'user_name'),'left');
		$select->join(array('shop_details'=>'user_details'), new Expression('shop_details.user_id=shop.user_id'),array('shop_city'=>'city','shop_state'=>'state','shop_country'=>'country','shop_address'=>'address'),'left');
		$select->where('order_delivery_details.admin_process_status="1"');
		$alllist = $this->tableGateway->selectWith($select);	
		return $alllist;
	}
}